package team.scau.laboratory_repair.common.quartz.jobs.repair;

import lombok.extern.apachecommons.CommonsLog;
import org.apache.commons.lang3.StringUtils;
import org.quartz.*;
import org.springframework.scheduling.quartz.QuartzJobBean;
import team.scau.laboratory_repair.common.message.MessageUtil;
import team.scau.laboratory_repair.common.utils.PropTool;

import java.util.Date;

/**
 * @author 30254
 * creadtedate: 2018/10/28
 */
@CommonsLog
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class SuperviseJob implements Job {

//    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        JobDataMap dataMap = context.getJobDetail().getJobDataMap();
        String phone = dataMap.getString("phone");
        if(StringUtils.isBlank(phone)) {
            return;
        }
        Long recordId = dataMap.getLong("recordId");
        int noAssignee = dataMap.getIntValue("noAssignee");
        int noProgress = dataMap.getIntValue("noProgress");
        boolean claimed = dataMap.getBoolean("claimed");

        log.info(new Date().toString());
        log.info("phone"+phone);
        log.info("recordId"+recordId);
        log.info("noAssignee"+noAssignee);
        log.info("noProgress"+noProgress);
        log.info("claimed"+claimed);
        Long noAssigneeLimit = PropTool.getLong("noAssigneeLimit");
        Long noProgressLimit = PropTool.getLong("noProgressLimit");
        if(!claimed) {
            if(noAssignee >= noAssigneeLimit){
                log.debug("未接手订单号"+recordId+"10天内没有接手");
//                MessageUtil.sendMessageNewItem(phone, "未接手","订单号"+recordId+"10天内没有接手");
            }
            dataMap.put("noAssignee", noAssignee + 1);
            context.put("noAssignee", noAssignee + 1);
            dataMap.put("noProgress", 0);
        } else {
            if(noProgress >= noProgressLimit){
                log.debug("已接手订单号"+recordId+"两周内没有进展");
//                MessageUtil.sendMessageNewItem(phone, "","订单号"+recordId+"");
            }
            dataMap.put("noProgress", noProgress + 1);
            dataMap.put("noAssignee", 0);
        }
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        executeInternal(context);
    }
}
